

package yf.mall.service.impl;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.dao.MallGoodsDao;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.MallGoodsService;
import yf.mall.service.UserService;
import yf.mall.vo.up.GoodsGroupUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import java.io.InputStream;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  yf.mall.vo.down.GoodsGroupDown;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.GoodsGroupDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import yf.mall.dao.GoodsGroupDao;
import yf.mall.entity.GoodsGroupEntity;
import yf.mall.vo.GoodsGroupVo;
import yf.mall.service.GoodsGroupService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Sun Sep 25 10:16:00 CST 2022
 */
@Service
public class GoodsGroupServiceImpl extends BaseServiceImpl<GoodsGroupEntity> implements GoodsGroupService {

    private GoodsGroupDao dao;


    @Autowired
    public void setDao(GoodsGroupDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<GoodsGroupVo> findVo(GoodsGroupVo vo){
        return dao.findVo(vo);
    }

    @Override
    public GoodsGroupVo getVo(Long id){
        return dao.getVo(id);
    }

    @Autowired
    private MallGoodsService goodsService;

    @Autowired
    private MallGoodsDao goodsDao;

    @Autowired
    private UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(GoodsGroupVo vo){
        if(vo.getGoodsGroup().getGoodsName()==null||vo.getGoodsGroup().getUserCode()==null){
           throw new RRException("数据有误!");
        }
        MallGoodsEntity goodsEntity = goodsDao.findByName(vo.getGoodsGroup().getGoodsName());
        UserEntity user = userService.findByCode(vo.getGoodsGroup().getUserCode());
        if(goodsEntity==null||user==null){
            throw new RRException("数据有误!");
        }
        vo.getEntity().setGoodsId(goodsEntity.getId());
        vo.getEntity().setUserId(user.getId());
        vo.getEntity().setUserName(user.getName());
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(GoodsGroupVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<GoodsGroupVo> list) {
        try {
            List<GoodsGroupDown> datas = list.stream().map(item -> new GoodsGroupDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), GoodsGroupDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, GoodsGroupUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<GoodsGroupUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(GoodsGroupUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/

    @Override
    public GoodsGroupEntity findByUserIdAndGoodsId(Long userId, Long goodsId) {
        return dao.findByUserIdAndGoodsId(userId,goodsId);
    }

    @Override
    public GoodsGroupEntity addGroup(Long userId, Long goodsId) {
        UserEntity userEntity = userService.get(userId);
        MallGoodsEntity goodsEntity = goodsService.get(goodsId);
        if(goodsEntity.getVipProfit()==null){
            return null;
        }
        if(userEntity.getLevelId()>=46&&userEntity.getLevelId()<=49){
//            throw new RRException("暂未开放");
            return null;
        }
        GoodsGroupEntity group = findByUserIdAndGoodsId(userId,goodsId);
        if(group!=null&&group.getProfit()!=null){
//            throw new RRException("开团已成功");
            return group;
        }
        if(group!=null&&group.getProfit()==null){
//            throw new RRException("审核中..");
            return group;
        }



        group = new GoodsGroupEntity(userId,goodsEntity.getName(),goodsId,userEntity.getCode(),userEntity.getName(),goodsEntity.getTitle()+"团购");
        //自动设置利润
        group.setProfit(goodsEntity.getVipProfit());
        save(group);
        return group;
    }
}
